#include "Log.h"
#include "loghandler.h"
#include "jid.h"
#include "client.h"
#include "eventhandler.h"
#include "connectionlistener.h"
#include "Poco/Util/Application.h"
#include "iostream"



using namespace cm_base;
using namespace cm_xmpp;

class TestLogHandler: public LogHandler
{
public:
    virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
    {
        g_log_root.warning(message);
    }
};

class TestConnectionListener: public ConnectionListener
{
public:
    void onConnect()
    {

    }

    void onDisconnect( int e )
    {

    }

    bool onTLSConnect(const CertInfo& info )
    {

        return true;
    }
};

class PingPongHandler: public EventHandler
{
public:
    void handleEvent( const Event& event )
    {
        std::cout<<"============handleEvent"<<std::endl;
        if( event.eventType() == Event::PingPong ){
            std::cout<<"Event::PingPong"<<std::endl;
        }

        if( event.eventType() == Event::PingError ){
            std::cout<<"Event::PingError"<<std::endl;
        }
    }
protected:
private:
};

int main()
{
    Poco::Util::Application* p_app = new Poco::Util::Application;
    p_app->loadConfiguration("c:/test/test_case.xml");

    ByteBufferPool bpool;
    bpool.setup(20, 1024);

    TestLogHandler tl;
    TestConnectionListener tc;

    JID jid("cmjabber_b@jabber.org");
    //JID jid("cnshibo@gmail.com");
    //JID jid("testa@boshi");

    Client* p_client = new Client(jid, "123456", 5222);
    //Client* p_client = new Client(jid, "71100313", 5222);
    //Client* p_client = new Client(jid, "loveyou", 5222);

    p_client->setBufferPool(&bpool);

    p_client->registerConnectionListener(&tc);
    p_client->logInstance().registerLogHandler(LogLevelDebug, LogAreaAll, &tl);


    p_client->disco()->setVersion( "ChipMind", CM_VERSION, "Linux");
    p_client->disco()->setIdentity("client", "bot");

    p_client->connect();


    PingPongHandler pph;

    std::string input;
    while (std::cin>>input)
    {
        std::cout<<input<<std::endl;
        //should use full jid of peer
        JID peer("cmjabber_b@jabber.org/pidgin");

        p_client->xmppPing(peer, &pph);

    }
    return 0;
}
